﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace Manazra.Models
{
    public partial class Blog
    {
        //validates model settings
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        //returns true if the user logged in is the author of this blog
        public bool IsAuthor(string userName)
        {
            return Author.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
        }

        //returns any model setting violations
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Author))
                yield return new RuleViolation("Author required", "Author");

            if (String.IsNullOrEmpty(Title))
                yield return new RuleViolation("Title of the blog required", "Title");

            yield break;
        }

        //override method for model validation
        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rules violations prevent saving");
        }
    }
}
